package br.com.efacta.bagarita.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.efacta.bagarita.domain.Discipline;
import br.com.efacta.bagarita.domain.repos.DisciplineRepository;

@Controller
@RequestMapping("/admin/discipline")
public class DisciplineController {

    @Autowired
    private DisciplineRepository repository;

    @RequestMapping(method = RequestMethod.GET, value = "/add")
    public ModelAndView add() {
        Map<String, Object> context = new HashMap<String, Object>();
        context.put("persisted", Boolean.FALSE);
        context.put("discipline", new Discipline());
        return new ModelAndView("admin/discipline-edit", context);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/edit/{disciplineId}")
    public ModelAndView editDiscipline(@PathVariable("disciplineId") String disciplineId) {
        Map<String, Object> context = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(disciplineId)) {
            Discipline discipline = repository.findOne(disciplineId);
            context.put("discipline", discipline);
        }
        context.put("persisted", Boolean.FALSE);
        return new ModelAndView("admin/discipline-edit", context);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public ModelAndView saveDiscipline(@ModelAttribute("discipline") Discipline discipline) {
        if (StringUtils.isBlank(discipline.getId())) {
            discipline.setId(null);
        }
        discipline = repository.save(discipline);
        Map<String, Object> context = new HashMap<String, Object>();
        context.put("persisted", Boolean.TRUE);
        context.put("discipline", discipline);
        return new ModelAndView("admin/discipline-edit", context);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public ModelAndView listAllDisciplines() {
        Iterable<Discipline> disciplines = repository.findAll();
        List<Discipline> disciplinesList = new ArrayList<Discipline>();
        if (disciplines != null) {
            for (Discipline discipline : disciplines) {
                disciplinesList.add(discipline);
            }
        }
        return new ModelAndView("admin/discipline-list", "disciplines", disciplinesList);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/delete/{disciplineId}")
    public ModelAndView deleteDiscipline(@PathVariable("disciplineId") String disciplineId) {
        if (StringUtils.isNotBlank(disciplineId)) {
            Discipline discipline = repository.findOne(disciplineId);
            if (discipline != null) {
                repository.delete(discipline);
            }
        }
        return new ModelAndView("redirect:/admin/discipline/list");
    }
}
